package com.bawei.module_service.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_service.R
import com.bawei.module_service.model.MenuItem

class MenuAdapter(private val menuItems : List<MenuItem>,private val onItemClickListener: (MenuItem) -> Unit ) : RecyclerView.Adapter<MenuAdapter.MenuViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MenuViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_service, parent, false)
        return MenuViewHolder(view)
    }

    override fun getItemCount(): Int {
        return menuItems.size
    }

    override fun onBindViewHolder(holder: MenuViewHolder, position: Int) {
        val menuItem = menuItems[position]
        holder.iconImageView.setImageResource(menuItem.iconResId)
        holder.titleTextView.text = menuItem.title

        // 设置 item 点击事件
        holder.itemView.setOnClickListener {
            onItemClickListener.invoke(menuItem)
        }
    }


    class MenuViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val iconImageView: ImageView = itemView.findViewById(R.id.icon_service)
        val titleTextView: TextView = itemView.findViewById(R.id.text_service)
    }
}